from rest_framework import serializers
from .models import *


class TestInfoSerializers(serializers.ModelSerializer):
    # 如果是多对多关系, 那么还需加上：mary=True, 插值时execute_user = username, 不能为 uid
    execute_user = serializers.SlugRelatedField(slug_field='username', queryset=UserInfo.objects.all())
    # 外键允许为空
    police = serializers.SlugRelatedField(slug_field='nickname', read_only=True)
    class Meta:
        model = TestInfo
        fields = "__all__"

    # 创建时调用
    def create(self, validated_data):
        # 当外键允许为空时, 必须手动添加
        if self.initial_data.get('police'):
            validated_data['police'] = Police.objects.filter(nickname=self.initial_data.get('police')).first()
        info = TestInfo.objects.create(**validated_data)
        return info

    # 更新时调用
    def update(self, instance, validated_data):
        if self.initial_data.get('police'):
            validated_data['police'] = Police.objects.filter(nickname=self.initial_data.get('police')).first()

        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance


class BaseScheduleInfoSerializers(serializers.ModelSerializer):
    # 如果是多对多关系, 那么还需加上：mary=True, 插值时execute_user = username, 不能为 uid
    execute_user = serializers.SlugRelatedField(slug_field='username', queryset=UserInfo.objects.all())
    # 外键允许为空
    police = serializers.SlugRelatedField(slug_field='nickname', read_only=True)
    class Meta:
        model = BaseScheduleInfo
        fields = "__all__"

    # 创建时调用
    def create(self, validated_data):
        # 当外键允许为空时, 必须手动添加
        if self.initial_data.get('police'):
            validated_data['police'] = Police.objects.filter(nickname=self.initial_data.get('police')).first()
        info = BaseScheduleInfo.objects.create(**validated_data)
        return info

    # 更新时调用
    def update(self, instance, validated_data):
        if self.initial_data.get('police'):
            validated_data['police'] = Police.objects.filter(nickname=self.initial_data.get('police')).first()

        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance


class UserInfoSerializers(serializers.ModelSerializer):

    class Meta:
        model = UserInfo
        fields = "__all__"


class CaseManageSerializers(serializers.ModelSerializer):

    class Meta:
        model = CaseManage
        fields = "__all__"


class PackMangeSerializers(serializers.ModelSerializer):

    class Meta:
        model = PackMange
        fields = "__all__"


class VersionManageSerializers(serializers.ModelSerializer):
    # read_only 不设置为True 插入时必须携带外键数据
    pack = PackMangeSerializers(many=True, read_only=True)

    class Meta:
        model = VersionManage
        fields = "__all__"

    def create(self, validated_data):
        info = VersionManage.objects.create(**validated_data)
        return info


class RegressionTestInfoSerializers(serializers.ModelSerializer):
    regression = serializers.SlugRelatedField(slug_field='uid', queryset=Regression.objects.all())
    execute_user = serializers.SlugRelatedField(slug_field='username', queryset=UserInfo.objects.all())

    class Meta:
        model = RegressionTestInfo
        fields = "__all__"

    def create(self, validated_data):
        info = RegressionTestInfo.objects.create(**validated_data)
        return info


class RegressionSerializers(serializers.ModelSerializer):
    version_name = serializers.SlugRelatedField(slug_field='version_name', queryset=VersionManage.objects.all())
    execute_user = serializers.SlugRelatedField(slug_field='username', queryset=UserInfo.objects.all())
    # 外键允许为空
    police = serializers.SlugRelatedField(slug_field='nickname', read_only=True)

    class Meta:
        model = Regression
        fields = "__all__"

    def create(self, validated_data):
        # 当外键允许为空时, 必须手动添加
        if self.initial_data.get('police'):
            validated_data['police'] = Police.objects.filter(nickname=self.initial_data.get('police')).first()
        info = Regression.objects.create(**validated_data)
        return info


class PoliceSerializers(serializers.ModelSerializer):
    class Meta:
        model = Police
        fields = "__all__"
